package in.muton.edushare.components.messaging.sendpanel;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.auth.api.Authentication;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.processing.objects.UserDataHandler;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.JavascriptPackageResource;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.model.IModel;

import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Enrolments;
import com.edushare.common.vo.Groups;
import com.edushare.common.vo.Messages;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class ActionPanel extends EdusharePanel {

	private static final Log log = LogFactory.getLog(ActionPanel.class);

	public ActionPanel(String id, Universities pageOwner, EduShareBasePage page) {
		super(id, new CayenneModel<Universities>(pageOwner), page);
		this.page = page;
		recType = Messages.RECEIVER_TYPE_UNIVERSITY;
		recId = pageOwner.getUniversityId();
		assgnPanel = false;
		Users loginUser = EduShareSession.get().getLoggedInUser();
		Authentication auth = new Authentication(pageOwner, loginUser);
		if (auth.isAllowed(Authentication.MSG_SND)) {
			msgPanel = true;
		}

		if (auth.isAllowed(Authentication.CAL_NEW)) {
			schPanel = true;
		}
		actionPanel(pageOwner);
	}

	public ActionPanel(String id, Departments pageOwner, EduShareBasePage page) {
		super(id, new CayenneModel<Departments>(pageOwner), page);
		this.page = page;
		recType = Messages.RECEIVER_TYPE_DEPARTMENT;
		recId = pageOwner.getDepartmentId();
		Users loginUser = EduShareSession.get().getLoggedInUser();
		Authentication auth = new Authentication(pageOwner, loginUser);
		assgnPanel = false;
		if (auth.isAllowed(Authentication.MSG_SND)) {
			msgPanel = true;
		}
		if (auth.isAllowed(Authentication.CAL_NEW)) {
			schPanel = true;
		}
		actionPanel(pageOwner);
	}

	public ActionPanel(String id, CourseInstances pageOwner,
			EduShareBasePage page) {
		super(id, new CayenneModel<CourseInstances>(pageOwner), page);
		this.page = page;
		recType = Messages.RECEIVER_TYPE_COURSE;
		recId = pageOwner.getCourseInstanceId();
		Users loginUser = EduShareSession.get().getLoggedInUser();
		Authentication auth = new Authentication(pageOwner, loginUser);
		assgnPanel = false;
		if (auth.isAllowed(Authentication.MSG_SND)) {
			msgPanel = true;
		}

		if (auth.isAllowed(Authentication.CAL_NEW)) {
			schPanel = true;
		}
		actionPanel(pageOwner);
	}

	public ActionPanel(String id, Users pageOwner, EduShareBasePage page) {
		super(id, new CayenneModel<Users>(pageOwner), page);
		this.page = page;
		log.debug("Page Owner = " + pageOwner.getCompleteUserName());
		userModel = new CayenneModel<Users>(pageOwner);
		recType = Messages.RECEIVER_TYPE_USER;
		recId = pageOwner.getUserId();

		UserDataHandler ud = new UserDataHandler(userModel);
		if (ud.getCourseInstByRole(Enrolments.TEACHER).getObject().size() > 0
				|| ud.getCourseInstByRole(Enrolments.TEACHING_ASSISTANT)
						.getObject().size() > 0)
			assgnPanel = true;

		Users loginUser = EduShareSession.get().getLoggedInUser();
		Authentication auth = new Authentication(pageOwner, loginUser);

		if (pageOwner != loginUser && auth.isAllowed(Authentication.MSG_SND))
			msgPanel = true;

		if (pageOwner == loginUser) {
			schPanel = true;
		}
		actionPanel(pageOwner);
	}
	
	public ActionPanel(String id, Groups pageOwner, EduShareBasePage basepage){
		super(id,new CayenneModel<Groups>(pageOwner),basepage);
		this.page = basepage;
		recType = Messages.RECEIVER_TYPE_GROUP;
		recId = pageOwner.getGroupId();
		msgPanel = true;
		actionPanel(pageOwner);
	}

	private void actionPanel(Object pageOwner) {
		add(CSSPackageResource.getHeaderContribution(ActionPanel.class,
				"ActionPanel.css"));
		add(JavascriptPackageResource.getHeaderContribution(ActionPanel.class,
				"ActionPanel.js"));

		if (msgPanel) {
			add(new MessagePanel("msgPanel",
					new CayenneModel<Object>(pageOwner),page));
		} else
			add(new EmptyPanel("msgPanel"));
		if (schPanel) {
			add(new SchedulePanel("schPanel", new CayenneModel<Object>(
					pageOwner), recType, recId, basePage));
		} else
			add(new EmptyPanel("schPanel"));
		if (assgnPanel)
			add(new AssgnPanel("assgnPanel"));
		else
			add(new EmptyPanel("assgnPanel"));
	}
	
	private EduShareBasePage page;

	private int recId = 0;
	private int recType = 0;
	boolean msgPanel = false;
	boolean assgnPanel = false;
	boolean schPanel = false;
	private static final long serialVersionUID = 4358965289732951579L;
	public IModel<Users> userModel = null;
}
